<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Emailreport extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('sys/emailreport_model');
		$this->load->model($this->session->userdata('formodel'));
		$this->load->model('wus');
		$this->load->library('upload');
		$this->load->library('variable');

		$config['protocol'] = "smtp";
		$config['smtp_host'] = "ssl://smtp.gmail.com";
		$config['mailpath'] = "assets/tmp";
		$config['smtp_port'] = 465;
		$config['smtp_user'] = "tetsuyakuroko101@gmail.com";
		$config['smtp_pass'] = "dwuydqsvumopnxdk";
		$config['smtp_timeout'] = 5;
		$config['charset'] = "utf-8";
		$config['mailtype'] = "html";
		$config['newline'] = "\r\n";
		$config['crlf'] = "\n";
		$this->email->initialize($config);

	}

	public function index(){

		$this->load->view('layouts/header',$this->variable->load_data($this->session->userdata('title')));

	    $this->load->view($this->session->userdata('formain'),array('report'	=>	$this->emailreport_model->getReports(),
																	'schedule'	=>	$this->emailreport_model->getSchedule()));

	    $this->load->view('layouts/footer');

	}

	public function add_form(){
		$this->load->view($this->session->userdata('foradd'), array('email'	=>	$this->emailreport_model->getEmails(),
																	'report'	=>	$this->emailreport_model->getReports(),
																	'schedule'	=>	$this->emailreport_model->getSchedule()));
	}

	public function edit_form($id){
		$this->load->view($this->session->userdata('foredit'),array('email'	=>	$this->emailreport_model->getEmail($id),
																	'report'	=>	$this->emailreport_model->getReports(),
																	'schedule'	=>	$this->emailreport_model->getSchedule()));
	}
	public function view_form($id){
		$this->load->view($this->session->userdata('forview'),array(
				'role_data' => $query
			));
	}

	public function search(){
		$q = $this->emailreport_model->search($this->input->post('name'),$this->input->post('email'));
		$c = 0; $tmp = "";
		if(!empty($q)){
			foreach($q as $w){
				$tmp .= "<tr><input value=".$w->email_id." type='hidden' id='moduleId'/><td>".$w->name."</td><td align='right'>".$w->email.'</td><td align="center"><button type="button" class="btn btn-outline btn-sm btn-primary btn-xs edit-tab" data-toggle="tab" role="tab" href="#view" title="View"><i class="fa fa-pencil fa-lg"></i></button></td></tr>';
				$c++;
			}
			echo json_encode(array('result' => $tmp,'count' => $c));
		}else{
			echo json_encode(array('result' => NULL,'count' => '0'));
		}
		
	}

	public function insert(){
		$email = $this->input->post('email');
		$e_flag = ($this->input->post('enable_flag') == 'ok' ? 'Y' : 'N');
		$data = array($email,
					  $e_flag,
					  $this->session->userdata('user_id'));
		$q = $this->emailreport_model->insert($data);
	}

	public function insertReport(){
		$email_id = $this->input->post('1email_id');
		$report_id = $this->input->post('1report_id');
		$times = $this->input->post('1times');
		$schedule = $this->input->post('1schedule');
		$e_flag = ($this->input->post('1enable_flag') == 'ok' ? 'Y' : 'N');
		$time = $this->input->post('time');
		$ampm = $this->input->post('ampm');
		$filetype = $this->input->post('1filetype');
		$data = array($email_id,
					  $report_id,
					  $times,
					  $schedule,
					  $e_flag,
					  $time,
					  $ampm,
					  $filetype);
		$q = $this->emailreport_model->insertReport($data);
		if($q['err'] == 1){
			echo json_encode($q);
		}else{
			$this->reportdtl($email_id);
		}
	}

	public function reportdtl($id){
		$this->emailreport_model->getReportDtl($id);
	}

	public function email_check_for_schedule(){
		$resulta = $this->emailreport_model->getSchedules();
		foreach($resulta as $r){
			if((int) $r->today >= ($r->times == 'once' ? ((int) $r->description) : (int) ($r->description / 2))){
				$ex = explode('|',$r->parameter);
				$url;
				if($r->filetype == 'pdf'){
					$url = $ex[1];
				}else{
					$url = $ex[0];
				}
				$result = file_get_contents(site_url($url));
				$data = array($r->email_dtl_id, $r->email_id, 'Pending', $result,'I');
				$this->emailreport_model->emailLogs($data);
				if(empty($result)){
					$data = array($r->email_dtl_id, $r->email_id, 'Failed', $result,'I');
					$this->emailreport_model->emailLogs($data);
				}else{
					$d = array('email' => $r->email, 'name' => $r->name,'report' => $r->report_name, 'attachment' => $result);
					$e = $this->sendEmail($d);
					if($e){
						$data = array($r->email_dtl_id, NULL, 'Complete', $result,'U');
						$this->emailreport_model->emailLogs($data);
					}else{
						$data = array(NULL, NULL, 'Failed', $result,'U');
						$this->emailreport_model->emailLogs($data);
					}
				}
				//echo "<pre>".print_r($result,1)."</pre>";
			}
		}	
	}

	public function email_check_for_delete(){
		$delete = $this->emailreport_model->getDataToDelete();
		foreach($delete as $d){
			if($d->deleted === 0){
				unlink('assets/tmp/'.$d->attachment);
				$data = array($d->email_dtl_id, NULL, NULL, $d->attachment,'A');
				$this->emailreport_model->emailLogs($data);
			}
		}
	}

	public function sendEmail($data){
 		$msg = 'test';
 		//var_dump($data);
		$this->email->from('kyogojowatersystem', $data['name']);
		//$list = array($this->input->post('email_to'));
		$this->email->to($data['email']);
		$this->email->subject('Kyogojo Account Registration');
		$this->email->message($msg);
		$this->email->attach('assets/tmp/'.$data['attachment']);
		if ($this->email->send()) {
			//echo json_encode(array('msg' => 'success'));
			return TRUE;
		} else {
			//echo json_encode(array('msg' => 'fail'));
			return FALSE;
			//show_error($this->email->print_debugger());
		}
	}

	public function stats(){
		$tmp = "";
		$q = $this->emailreport_model->getStatus();
		foreach($q as $s):
			$badge = "";
			if(strtolower($s->status) == "failed"){
				$badge = "danger";
			}else if(strtolower($s->status) == "pending"){
				$badge = "warning";
			}else if(strtolower($s->status) == "complete" || strtolower($s->status) == "success"){
				$badge = "primary";
			}
			$tmp .= "<tr><td>".$s->name."</td><td>".$s->email."</td><td>".$s->report_name."</td><td align='center'><span class='label label-".$badge."'>".$s->status."</span></td><td>".$s->last_try."</td></tr>";
		endforeach;
		echo json_encode(array('result' => $tmp));
	}

	public function updateReportDtl(){
		$email_dtl_id = $this->input->post('email_dtl_id');
		$report_code = $this->input->post('report_id');
		$times = $this->input->post('times');
		$schedule = $this->input->post('schedule');
		$filetype = $this->input->post('filetype');
		$ena_flag = ($this->input->post('enable_flag') == 'ok' ? 'Y' : 'N');
		$data = array($email_dtl_id,$report_code,$times,$schedule,$ena_flag,$filetype);
		echo json_encode($this->emailreport_model->updateSave($data));
	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */